<template>
  <div>
    <van-button
      class="follow-btn"
      type="info"
      color="#3296fa"
      round
      size="small"
      icon="plus"
      v-if="value"
      @click="followClick"
      :loading="isLoading"
      >关注</van-button
    >
    <van-button
      class="follow-btn"
      round
      size="small"
      v-else
      @click="followClick"
      :loading="isLoading"
      >已关注</van-button
    >
  </div>
</template>

<script>
import { addFollow, deleteFollow } from "@/api/user";
export default {
  name: "followUser",
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    authorId: {
      type: [String, Number],
      required: true,
    },
  },
  data() {
    return {
      isLoading: false,
    };
  },
  methods: {
    // 关注列表
    async followClick() {
      if (!this.$store.getters.token) this.$toast.fail("未登录请登录");
      this.isLoading = true;

      try {
        if (this.value) {
          await deleteFollow(this.authorId);
        } else {
          await addFollow({
            target: this.authorId,
          });
        }
        /* this.articleInfo.is_followed = !this.articleInfo.is_followed; */
        this.$emit("input", !this.value);
      } catch (error) {
        console.log(error);
        this.$toast.fail("关注失败");
      }
      this.isLoading = false;
    },
  },
};
</script>

<style>
</style>